package userServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swim2.entitybeans.Feedback;
import swim2.sessionbeans.*;

public class ViewFeedbackServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public ViewFeedbackServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		try{
			Context ctx = getInitialContext();			
			UserSystemRemote userSystemRemote = (UserSystemRemote) ctx.lookup("userJNDI");
			int navId = Integer.parseInt(request.getParameter("navId"));
			int userId = Integer.parseInt(request.getParameter("userId"));
			String feedTitle = request.getParameter("feedTitle");
			Feedback feedback = userSystemRemote.getFeedback(feedTitle);
			String userMail = userSystemRemote.getMail(navId);
			String userPassword = userSystemRemote.getPassword(navId);
			String senderNameSurname = userSystemRemote.getCompleteName(feedback.getAuthorId());
			String ability = userSystemRemote.getAbilityName(feedback.getAbilityId());
			
			request.setAttribute("userId", userId);
			request.setAttribute("senderNameSurname", senderNameSurname);
			request.setAttribute("ability", ability);
			request.setAttribute("navId", navId);
			request.setAttribute("feedback", feedback);
			request.setAttribute("userMail", userMail);
			request.setAttribute("userPassword", userPassword);
			RequestDispatcher dispatcher =
			request.getRequestDispatcher("/UserJsp/ViewFeedback.jsp");
			dispatcher.forward(request, response);	
		}
		catch (Exception e) {
			e.printStackTrace();
			out.print("Error: exception");
		}
	}

	static public Context getInitialContext() throws Exception{
		Hashtable<String,String> env = new Hashtable<String,String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");
		return new InitialContext(env);
	}
}

